package com.hejjon.knowledgebase.interceptor;

import com.hejjon.knowledgebase.annotation.EventLog;
import com.hejjon.knowledgebase.service.UserEventLogService;
import com.hejjon.knowledgebase.util.EventParamsHolder;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户操作日志切面
 * @author:
 * @date: 2022-12-07 09:25:29
 * @since: 1.0
 */
@Component
@Aspect
@Order(20)
public class UserEventLogAspect {

    @Resource
    private UserEventLogService userEventLogService;

    /**
     * 方法上标注 @EventLog 注解
     * @param e
     */
    @Pointcut("@annotation(e)")
    public void eventLogPoint(EventLog e) {
    }

    /**
     * 记录日志前 设置默认参数
     * @param e
     */
    @Before(value = "eventLogPoint(e)", argNames = "e")
    public void beforeLogRecord(EventLog e) {
        EventParamsHolder.remove();
        // 设置默认参数
        EventParamsHolder.setDefaultEventParams();
    }

    /**
     *
     * @param e
     */
    @AfterReturning(pointcut = "eventLogPoint(e)", argNames = "e")
    public void afterLogRecord(EventLog e) {
        userEventLogService.recordLog(e.value(), true);
    }

    /**
     *
     * @param e
     */
    @AfterThrowing(pointcut = "eventLogPoint(e)", argNames = "e")
    public void afterLogRecordThrowing(EventLog e) {
        userEventLogService.recordLog(e.value(), false);
    }



}
